package com.admin.controller.system;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.admin.common.constant.BizConstant;
import com.admin.common.enums.EnumMenuType;
import com.admin.common.enums.EnumResult;
import com.admin.common.util.YsStringUtil;
import com.admin.controller.base.BaseController;
import com.admin.domain.MenuDO;
import com.admin.req.system.SaveMenuReq;
import com.admin.req.system.UpdateMenuReq;
import com.admin.service.MenuService;
import com.admin.vo.PageVO;
import com.admin.vo.ResultVO;
import com.admin.vo.SimpleResultVO;
import com.admin.vo.system.AddMenuVO;
import com.admin.vo.system.ListMenuListVO;
import com.admin.vo.system.MenuPermCanSeeVO;
import com.admin.vo.system.MenuTypeSelectedVO;
import com.admin.vo.system.UpdateMenuVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 权限管理
 * 
 * @author ZY
 * @date 2018/10/18
 */
@Api(description = "权限管理")
@RestController
@RequestMapping(value = "/menu")
public class MenuController extends BaseController {

    @Autowired
    private MenuService menuService;

    @ApiOperation(value = "权限列表")
    @RequiresPermissions("menu:list")
    @GetMapping(value = "/list")
    @ApiImplicitParams(
        value = {@ApiImplicitParam(name = "pageIndex", value = "当前页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query")})
    public ResultVO<PageVO<ListMenuListVO>> listMenu(Integer pageIndex, Integer pageSize) {
        return menuService.getMenuVOs(pageIndex, pageSize, null);
    }

    @ApiOperation(value = "到新增权限页")
    @RequiresPermissions("menu:save")
    @GetMapping(value = "/toAdd")
    @ApiImplicitParam(name = "parentId", value = "权限父id，不传为一级目录", dataType = "long", paramType = "query")
    public ResultVO<AddMenuVO> toAddMenu(Long parentId) {
        ResultVO<AddMenuVO> result = new ResultVO<>();

        AddMenuVO addMenuVO = new AddMenuVO();
        if (parentId == null || parentId == 0L) {
            addMenuVO.setParentId(BizConstant.PARENT_ID_ZERO_LONG);
            addMenuVO.setParentName(BizConstant.ROOT_MENU_NAME);
        } else {
            MenuDO menuDO = menuService.selectByPrimaryKey(parentId);
            addMenuVO.setParentId(parentId);
            addMenuVO.setParentName(menuDO.getName());
        }
        addMenuVO.setMenuTypeVOs(getMenuTypeSelected(EnumMenuType.NAV.getKey()));

        result.setData(addMenuVO);
        result.setCode(EnumResult.SUCCESS.getCode());
        result.setMsg(EnumResult.SUCCESS.getMsg());
        return result;
    }

    @ApiOperation(value = "新增权限")
    @RequiresPermissions("menu:save")
    @PostMapping(value = "/save")
    public SimpleResultVO saveMenu(@RequestBody SaveMenuReq req) {
        MenuDO menuDO = new MenuDO();
        BeanUtils.copyProperties(req, menuDO);
        return menuService.saveMenuDO(menuDO);
    }

    @ApiOperation(value = "到修改权限页")
    @RequiresPermissions("menu:list")
    @GetMapping(value = "/toUpdate")
    @ApiImplicitParam(name = "menuId", value = "权限id", required = true, dataType = "long", paramType = "query")
    public ResultVO<UpdateMenuVO> toUpdateMenu(Long menuId) {
        ResultVO<UpdateMenuVO> result = new ResultVO<UpdateMenuVO>();

        UpdateMenuVO updateMenuVO = new UpdateMenuVO();
        MenuDO menuDO = menuService.selectByPrimaryKey(menuId);
        BeanUtils.copyProperties(menuDO, updateMenuVO);
        if (menuDO.getParentId().equals(BizConstant.PARENT_ID_ZERO_LONG)) {
            updateMenuVO.setParentName(BizConstant.ROOT_MENU_NAME);
        } else {
            MenuDO parentMenuDO = menuService.selectByPrimaryKey(menuDO.getParentId());
            updateMenuVO.setParentName(parentMenuDO.getName());
        }
        updateMenuVO.setMenuTypeVOs(getMenuTypeSelected(menuDO.getType()));

        result.setData(updateMenuVO);
        result.setCode(EnumResult.SUCCESS.getCode());
        result.setMsg(EnumResult.SUCCESS.getMsg());
        return result;
    }

    @ApiOperation(value = "修改权限")
    @RequiresPermissions("menu:update")
    @PostMapping(value = "/update")
    public SimpleResultVO updateMenu(@RequestBody UpdateMenuReq req) {
        MenuDO menuDO = new MenuDO();
        BeanUtils.copyProperties(req, menuDO);
        return menuService.updateMenuDO(menuDO);
    }

    @ApiOperation(value = "删除权限")
    @RequiresPermissions("menu:delete")
    @GetMapping(value = "/delete")
    @ApiImplicitParam(name = "menuId", value = "权限id", required = true, dataType = "long", paramType = "query")
    public SimpleResultVO deleteMenu(Long menuId) {
        SimpleResultVO result = new SimpleResultVO();

        menuService.deleteByPrimaryKey(menuId);

        result.setCode(EnumResult.SUCCESS.getCode());
        result.setMsg(EnumResult.SUCCESS.getMsg());
        return result;
    }

    @ApiOperation(value = "批量删除权限")
    @RequiresPermissions("menu:batchDelete")
    @PostMapping(value = "/batchDelete")
    @ApiImplicitParam(name = "menuIds", value = "一组权限id，英文逗号分隔", required = true, dataType = "String",
        paramType = "query")
    public SimpleResultVO deleteMenus(String menuIds) {
        return menuService.deleteByPrimaryKeys(YsStringUtil.splitToLong(menuIds, ","));
    }

    @ApiOperation(value = "查询用户可见权限")
     @RequiresPermissions("menu:getCanSeeMenu")
    @GetMapping(value = "/getCanSeeMenu")
    public ResultVO<MenuPermCanSeeVO> getCanSeeMenu() {
        ResultVO<MenuPermCanSeeVO> result = new ResultVO<MenuPermCanSeeVO>();

        ResultVO<List<String>> r = menuService.getCanSeeMenuPerms(getUserId());
        MenuPermCanSeeVO menuNamesCanSeeVO = new MenuPermCanSeeVO();
        menuNamesCanSeeVO.setMenuPerms(r.getData());

        result.setData(menuNamesCanSeeVO);
        result.setCode(EnumResult.SUCCESS.getCode());
        result.setMsg(EnumResult.SUCCESS.getMsg());
        return result;
    }

    /**
     * 筛选权限类型
     * 
     * @param menuTypeSelected
     * @return
     */
    private List<MenuTypeSelectedVO> getMenuTypeSelected(Integer menuTypeSelected) {
        List<MenuTypeSelectedVO> menuTypeVOs = new ArrayList<MenuTypeSelectedVO>();
        for (EnumMenuType e : EnumMenuType.values()) {
            MenuTypeSelectedVO menuTypeVO = new MenuTypeSelectedVO();
            menuTypeVO.setType(e.getKey());
            menuTypeVO.setTypeName(e.getText());
            if (e.getKey().equals(menuTypeSelected)) {
                menuTypeVO.setIsSelected(true);
            } else {
                menuTypeVO.setIsSelected(false);
            }
            menuTypeVOs.add(menuTypeVO);
        }
        return menuTypeVOs;
    }

}
